﻿using System;
using System.Web.Mail;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MLY;
using MLY.Dal;
using MLY.Bll;
using System.IO;

namespace MLY.Reports.DataReports
{
    public partial class LateOrdersReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // security - if the user is not logged in than he should not be here.
            if (!IsPostBack)
            {
                // checking logged in user
                if (clsUser.CurrentUserRoleId < 0)
                {
                    Response.RedirectTo("/LTR/Login.aspx?ReturnTo=" + Request.Url.ToString());
                }

                // direction
                if (SessParam.Culture == "he")
                {
                    //cssLiteral.Text = @"<link type=""text/css"" rel=""stylesheet"" href=""/Style/Rtl_Stylesheet.css"" />";
                    DirectionPanel.Direction = ContentDirection.RightToLeft;
                }
                else
                {
                    //cssLiteral.Text = @"<link type=""text/css"" rel=""stylesheet"" href=""/Style/Ltr_Stylesheet.css"" />";
                    DirectionPanel.Direction = ContentDirection.LeftToRight;
                }

                if (Request.QueryString["userRole"] != null)
                {
                    UserRoleHiddenField.Value = Request.QueryString["userRole"].ToString();
                }
                else
                {
                    UserRoleHiddenField.Value = "Client";
                }

                // DEMO CODE
                if ((!IsPostBack) && (Session["UserId"].ToString() != "39"))
                {
                    DemoPanel1.Visible = false;
                    DemoPanel2.Visible = false;
                    DemoPanel3.Visible = false;
                    DemoPanel4.Visible = false;
                    DemoPanel5.Visible = false;
                }
                // EOF DEMO CODE
            }
        }

        protected void ddlOrgs_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlOrgs.Items.Clear();
                ListItem all = new ListItem("All", "0");
                ddlOrgs.Items.Add(all);
                ddlOrgs.AppendDataBoundItems = true;
                if ((Request.QueryString["userRole"] != null) &&
                    (Request.QueryString["userRole"].ToString() == "Client"))
                {
                    ddlOrgs.DataSource = MLY.Bll.clsOrganization.GetContractorsByClientId((int)Session["OrganizationId"]);
                    ddlOrgs.DataBind();
                }
                else
                {
                    ddlOrgs.DataSource = MLY.Bll.clsOrganization.GetClientsByContractorId((int)Session["OrganizationId"]);
                    ddlOrgs.DataBind();
                }
            }
        }

        protected void GenerateReport(object Sender, EventArgs e)
        {
            List<TableOrganization> orgList = new List<TableOrganization>();
            int OrgId = int.Parse(ddlOrgs.SelectedValue);

            if (OrgId == 0)
            {
                OrgListPanel.Visible = true;
                if (UserRoleHiddenField.Value == "Client")
                {
                    orgList = MLY.Bll.clsOrganization.GetContractorsByClientId((int)Session["OrganizationId"]);

                }
                else
                {
                    orgList = MLY.Bll.clsOrganization.GetClientsByContractorId((int)Session["OrganizationId"]);

                }
            }
            else
            {
                orgList.Add(MLY.Bll.clsOrganization.GetOrganizationById(OrgId));
                OrgListPanel.Visible = false;
            }

            ReportTablesPanel.Visible = true;

            ReportTablesPanel.Visible = true;
            if (OrgListPanel.Visible)
            {
                ReportOrganizationsAnchorsRepeater.DataSource = orgList;
                ReportOrganizationsAnchorsRepeater.DataBind();
            }
            FullReportRepeater.DataSource = orgList;
            FullReportRepeater.DataBind();

            // DEMO CODE
            if (Session["UserId"].ToString() == "39")
            {
                DemoPanel1.Visible = false;
                DemoPanel2.Visible = false;
                DemoPanel3.Visible = false;
                DemoPanel4.Visible = false;
                DemoPanel5.Visible = false;
                DemoPanel6.Visible = true;
            }
            // EOF DEMO CODE

        }

        protected void OnFullRepeaterItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                string OrdId = (e.Item.FindControl("OrganizationIdHiddenField") as HiddenField).Value;
                string CustomerId;
                string ContructorId;
                if (UserRoleHiddenField.Value == "Client")
                {
                    CustomerId = Session["OrganizationId"].ToString();
                    ContructorId = OrdId;
                }
                else
                {
                    CustomerId = OrdId;
                    ContructorId = Session["OrganizationId"].ToString();
                }

                GridView WOGrid = (e.Item.FindControl("ActualReportGridview") as GridView);

                WOGrid.DataSource = MLY.Bll.ReportsGenerator.LateOrdersReport(CustomerId, ContructorId, ddlLateOrderColumnDate.SelectedValue, OpenOrdersCheckBox.Checked, ClosedCheckbox.Checked);
                WOGrid.DataBind();
            }
        }

        public string FormatDate(object Date)
        {
            string RetValue;

            try
            {
                RetValue = string.Format("{0:d}", DateTime.Parse(Date.ToString()));
            }
            catch
            {
                RetValue = ("");
            }
            return RetValue;


        }
    }
}
